/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.symbiosys.workbench.tests.adapters;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.progress.IDeferredWorkbenchAdapter;
import org.eclipse.ui.progress.IElementCollector;
import org.reliablesource.model.core.IReliableElement;
import org.reliablesource.model.core.IReliableNode;
import org.reliablesource.symbiosys.workbench.tests.TestPlugin;


/**
 *
 * MockSlowElementAdapter.java<br>
 * Created on: Jun 27, 2008<br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class MockSlowElementAdapter implements IDeferredWorkbenchAdapter,
    IAdaptable {
    private IReliableElement adapted;

    public MockSlowElementAdapter(IReliableElement _toAdapt) {
        Assert.isNotNull(_toAdapt);
        adapted = _toAdapt;
    }

    /**
     *
     * @param _object
     * @param _collector
     * @param _monitor
     */
    public void fetchDeferredChildren(Object _object,
        IElementCollector _collector, IProgressMonitor _monitor) {
        if (_object instanceof IReliableNode) {
            int nChildren = ((IReliableNode) _object).getChildren().length;

            _monitor.beginTask("Loading", nChildren);

            for (int i = 0; i < nChildren; i++) {
                pause(1200);
                _collector.add(((IReliableNode) _object).getChild(i), _monitor);
                _monitor.worked(1);

                if (_monitor.isCanceled())
                    break;
            }

            _monitor.done();
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.progress.IDeferredWorkbenchAdapter#getRule(java.lang.Object)
     */
    public ISchedulingRule getRule(Object _element) {
        return null;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.progress.IDeferredWorkbenchAdapter#isContainer()
     */
    public boolean isContainer() {
        if (adapted instanceof IReliableNode)
            return ((IReliableNode) adapted).getChildren().length > 0;

        return false;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(java.lang.Object)
     */
    public Object[] getChildren(Object _element) {
        if (_element instanceof IReliableNode)
            return ((IReliableNode) _element).getChildren();

        return TestPlugin.NULL_OBJECT;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(java.lang.Object)
     */
    public ImageDescriptor getImageDescriptor(Object _element) {
        return null;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.model.IWorkbenchAdapter#getLabel(java.lang.Object)
     */
    public String getLabel(Object _element) {
        Assert.isNotNull(_element);

        return ((IReliableElement) _element).getName();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
     */
    public Object getParent(Object _element) {
        Assert.isNotNull(_element);

        if (_element instanceof IReliableElement)
            return ((IReliableElement) _element).getParent();

        return TestPlugin.NULL_OBJECT;
    }

    /**
     *
     */
    private void pause(long _msec) {
        try {
            Thread.sleep(_msec);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
     */
    public Object getAdapter(Class _adapter) {
        return Platform.getAdapterManager().getAdapter(adapted, _adapter);
    }
}
